package com.inspur.edp.common.expr.ast;


import com.inspur.edp.common.expr.exception.ExpressException;
import com.inspur.edp.common.type.TypeRefer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lizhaorui
 * @description TODO
 * @date 2025/4/8 16:52
 */
public class VariableScope {

    private final List<VariableCollection> varNameTypes;

    public VariableScope() {
        this.varNameTypes = new ArrayList<>();
        this.beginVarScope(true);
    }

    public void beginVarScope(boolean isolate) {
        this.varNameTypes.add(new VariableCollection(isolate));
    }

    public void commitVarScope() {
        this.varNameTypes.remove(this.varNameTypes.size() - 1);
    }

    public void addVarType(String varName, TypeRefer typeRefer) {
        List<VariableCollection> variableNameList = this.varNameTypes;
        if (variableNameList.size() == 0) {
            throw new ExpressException(String.format("the context has not been initialized yet, so variables %s can not be added", varName));
        }
        boolean containVar = false;
        for (int i = variableNameList.size() - 1; i >= 0; i--) {
            VariableCollection variableData = variableNameList.get(i);
            if (variableData.contains(varName)) {
                containVar = true;
                break;
            } else {
                if (variableData.isIsolate()) {
                    containVar = false;
                    break;
                } else {
                    continue;
                }
            }
        }
        if (containVar) {
            throw new ExpressException(String.format("variable %s already exists.", varName));
        } else {
            int lastIndex = this.varNameTypes.size();
            VariableCollection lastData = this.varNameTypes.get(lastIndex - 1);
            lastData.addVar(varName, typeRefer);
        }
    }

    public void removeVar(String varName) {
        List<VariableCollection> variableNameList = this.varNameTypes;
        for (int i = variableNameList.size() - 1; i >= 0; i--) {
            VariableCollection data = variableNameList.get(i);
            if (data.contains(varName)) {
                data.removeVar(varName);
                return;
            }
        }
    }

    public TypeRefer getVarType(String varName) {
        List<VariableCollection> variableNameList = this.varNameTypes;
        for (int i = variableNameList.size() - 1; i >= 0; i--) {
            VariableCollection data = variableNameList.get(i);
            if (data.contains(varName)) {
                return data.get(varName);
            }
        }
        throw new ExpressException(String.format("variable %s is undefined .", varName));
    }

    public boolean containVarName(String varName) {
        List<VariableCollection> variableNameList = this.varNameTypes;
        for (int i = variableNameList.size() - 1; i >= 0; i--) {
            if (variableNameList.get(i).contains(varName)) {
                return true;
            }
        }
        return false;
    }

}

